// Copyright 2024 Xiguan Inc
// Author: xiguan
// Email: xiguan.teng@qq.com
// Created on 2024/8/7
// This is ...
//
#include "wc.h"

#include <cctype>
#include <iostream>
#include <sstream>

static std::vector<std::string> check_words(std::string &word) {
  int                      len = word.length();
  std::vector<std::string> ans;
  std::string              t;

  for (int idx = 0; idx < len; ++idx) {
    if (!std::isalpha(word[idx])) {
      if (t.length() != 0) {
        ans.push_back(t);
        t.clear();
      }
      continue;
    }

    t += word[idx];
  }

  if (t.length() != 0) {
    ans.push_back(t);
    t.clear();
  }

  return ans;
}

VecMapRet lab1_map(const std::string &filename,
                   const std::string &file_contend) {
  std::stringstream                        ss(file_contend);
  std::string                              word;
  std::vector<std::pair<std::string, int>> key_value;

  while (ss >> word) {
    auto temp = check_words(word);
    for (auto &x : temp) {
      key_value.push_back({x, 1});
    }
  }
  return key_value;
}

int lab1_reduce(const std::string word, std::vector<int> values) {
  return values.size();
}